import 'package:jump_scape/js_config.dart';
import 'package:objectbox/objectbox.dart';
import 'package:path_provider/path_provider.dart';

@Entity()
class MemberBox {
  @Id()
  int id = 0;

  String userId;
  String name;
  DateTime? birth;
  String country;
  String balance;
  String avatarUrl;
  String audioPath;
  bool isAutoTranslate;
  bool isUnlockTopic3;
  int gameCount;

  @Property(type: PropertyType.date)
  DateTime createDate;

  MemberBox({
    required this.id,
    required this.userId,
    required this.name,
    required this.birth,
    required this.country,
    required this.balance,
    required this.avatarUrl,
    required this.audioPath,
    required this.isAutoTranslate,
    required this.createDate,
    required this.isUnlockTopic3,
    required this.gameCount,
  });

  MemberBox.create({
    required this.userId,
    required this.name,
    required this.birth,
    required this.balance,
    required this.country,
    required this.avatarUrl,
  })  : createDate = DateTime.now(),
        isAutoTranslate = false,
        audioPath = '',
        isUnlockTopic3 = false,
        gameCount = 10;

  Future<String> audioPathReal() async {
    if (audioPath.startsWith(JsApp.fileToTempName)) {
      try {
        final tempDir = await getTemporaryDirectory();
        return audioPath.replaceFirst(JsApp.fileToTempName, tempDir.path);
      } catch (_) {}
    }
    return audioPath;
  }

  @override
  String toString() {
    return {
      'id': id,
      'userId': userId,
      'name': name,
      'birth': birth,
      'country': country,
      'balance': balance,
      'avatarUrl': avatarUrl,
      'audioPath': audioPath,
      'isAutoTranslate': isAutoTranslate,
      'isUnlockTopic3': isUnlockTopic3,
      'gameCount': gameCount,
    }.toString();
  }
}
